﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Xenta.Entities;
using Xenta.Enums;
using Xenta.Plugins;
using Xenta.Security;
using Xenta.Utils;

namespace Xenta.Operations
{
    #region __Report__

    public class GlobalDashboardReport : ReportOperation
    {
        #region Properties

        public override string MimeType
        {
            get
            {
                return String.Empty;
            }
        }

        #endregion

        #region Utilities

        protected override void Report(IUnitOfWork uow)
        {
            //TODO implement report
        }

        #endregion
    }

    public class CatalogDashboardReport : ReportOperation
    {
        #region Properties

        public override string MimeType
        {
            get
            {
                return String.Empty;
            }
        }

        #endregion

        #region Utilities

        protected override void Report(IUnitOfWork uow)
        {
            //TODO implement report
        }

        #endregion
    }

    public class FileSystemDashboardReport : ReportOperation,
        ISecuredOperation
    {
        #region Properties

        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("ADMIN");
            }
        }

        public override string MimeType
        {
            get
            {
                return "text/xml";
            }
        }

        #endregion

        #region Utilities

        protected override void Report(IUnitOfWork uow)
        {
            var xReport = new XElement("report");
            // storage info
            using(var ioc = uow.Infrastructure.DependencyResolver())
            {
                foreach(var i in uow.Execute(new ListFileStorages()).Result)
                {
                    var state = new Dictionary<String, String>();
                    try
                    {
                        var storage = ioc.Resolve(i.ID) as IFileStorage;
                        if(storage == null)
                            state.Add("IoC error", "Storage cannot be resolved.");
                        state.AddValues(storage.State);
                    }
                    catch(Exception ex)
                    {
                        state.Add("IoC exception", ex.Message);
                    }
                    var xStorage = new XElement("storage",
                        new XAttribute("name", i.Name),
                        new XAttribute("type", i.Type));
                    foreach(var kv in state)
                    {
                        xStorage.Add(new XElement("param",
                            new XAttribute("key", kv.Key),
                            new XText(kv.Value)));
                    }
                    xReport.Add(xStorage);
                }
            }

            // statistics info
            var xStat = new XElement("stat");
            xStat.Add(new XAttribute("fileSizeSum", Convert.ToInt64(uow.OrmQueryStateless("select sum(f.Size) from FileEntity f").UniqueResult())));
            xStat.Add(new XAttribute("fileTotalNum", Convert.ToInt32(uow.OrmQueryStateless("select count(f.EntityID) from FileEntity f").UniqueResult())));
            xStat.Add(new XAttribute("quotaAllocSum", Convert.ToInt64(uow.OrmQueryStateless("select sum(o.Quota) from FileOwnerEntity o").UniqueResult())));
            xReport.Add(xStat);
            xReport.Save(OutStream);
        }

        #endregion
    }

    public class SystemDashboardReport : ReportOperation,
        ISecuredOperation
    {
        #region Properties

        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("ADMIN");
            }
        }

        public override string MimeType
        {
            get
            {
                return "text/xml";
            }
        }

        #endregion

        #region Utilities

        protected override void Report(IUnitOfWork uow)
        {
            var xReport = new XElement("report");

            // system log info
            var xLog = new XElement("log",
                new XAttribute("totalRecs", Convert.ToInt32(uow.OrmQueryStateless("select count(r.EntityID) from LogRecordEntity r")
                    .UniqueResult())),
                new XAttribute("totalInfs", Convert.ToInt32(uow.OrmQueryStateless("select count(r.EntityID) from LogRecordEntity r where (r.Flags & :mask) = :mask")
                    .SetInt32("mask", (int)LogRecordFlags.Information)
                    .UniqueResult())),
                new XAttribute("totalWrns", Convert.ToInt32(uow.OrmQueryStateless("select count(r.EntityID) from LogRecordEntity r where (r.Flags & :mask) = :mask")
                    .SetInt32("mask", (int)LogRecordFlags.Warning)
                    .UniqueResult())),
                new XAttribute("totalErrs", Convert.ToInt32(uow.OrmQueryStateless("select count(r.EntityID) from LogRecordEntity r where (r.Flags & :mask) = :mask")
                    .SetInt32("mask", (int)LogRecordFlags.Error)
                    .UniqueResult())));
            xReport.Add(xLog);

            // component listing
            var xComponents = new XElement("components");
            foreach(var i in uow.Execute(new ListDependencies<IDependency>()).Result)
            {
                xComponents.Add(new XElement("component",
                    new XAttribute("id", i.ID),
                    new XAttribute("version", i.Version),
                    new XAttribute("name", i.Name),
                    new XAttribute("type", i.Type)));
            }
            xReport.Add(xComponents);
            xReport.Save(OutStream);
        }

        #endregion
    }

    public class ForumDashboardReport : ReportOperation
    {
        #region Properties

        public override string MimeType
        {
            get
            {
                return String.Empty;
            }
        }

        #endregion

        #region Utilities

        protected override void Report(IUnitOfWork uow)
        {
            //TODO implement report
        }

        #endregion
    }

    public class GlobalizationDashboardReport : ReportOperation
    {
        #region Properties

        public override string MimeType
        {
            get
            {
                return String.Empty;
            }
        }

        #endregion

        #region Utilities

        protected override void Report(IUnitOfWork uow)
        {
            //TODO implement report
        }

        #endregion
    }

    public class MembershipDashboardReport : ReportOperation
    {
        #region Properties

        public override string MimeType
        {
            get
            {
                return String.Empty;
            }
        }

        #endregion

        #region Utilities

        protected override void Report(IUnitOfWork uow)
        {
            //TODO implement report
        }

        #endregion
    }

    public class MessagingDashboardReport : ReportOperation
    {
        #region Properties

        public override string MimeType
        {
            get
            {
                return String.Empty;
            }
        }

        #endregion

        #region Utilities

        protected override void Report(IUnitOfWork uow)
        {
            //TODO implement report
        }

        #endregion
    }

    public class PublishingDashboardReport : ReportOperation
    {
        #region Properties

        public override string MimeType
        {
            get
            {
                return String.Empty;
            }
        }

        #endregion

        #region Utilities

        protected override void Report(IUnitOfWork uow)
        {
            //TODO implement report
        }

        #endregion
    }

    public class SalesDashboardReport : ReportOperation
    {
        #region Properties

        public override string MimeType
        {
            get
            {
                return String.Empty;
            }
        }

        #endregion

        #region Utilities

        protected override void Report(IUnitOfWork uow)
        {
            //TODO implement report
        }

        #endregion
    }

    #endregion
}
